package com.gingko.duominuo.ui.activity

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.observe
import androidx.recyclerview.widget.LinearLayoutManager
import com.gingko.duominuo.R
import com.gingko.duominuo.data.Resource
import com.gingko.duominuo.data.model.WorkOrderDetailVo
import com.gingko.duominuo.data.model.WorkOrderVo
import com.gingko.duominuo.databinding.ActivityRegisterBinding
import com.gingko.duominuo.databinding.ActivityWorkOrderRecordBinding
import com.gingko.duominuo.ui.adapter.WordOrderRecordAdapter
import com.gingko.duominuo.ui.adapter.WorkOrderAdapter
import com.gingko.duominuo.ui.widget.DefaultItemDecoration
import com.gingko.duominuo.ui.widget.EmptyLayout
import com.gingko.duominuo.ui.widget.MCountDownTimer
import com.gingko.duominuo.utils.StatusBarUtil
import com.gingko.duominuo.utils.toast
import com.gingko.duominuo.viewmodel.BaseViewModel
import com.gingko.duominuo.viewmodel.RegisterViewModel
import com.gingko.duominuo.viewmodel.WorkOrderRecordViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class WorkOrderRecordActivity : BaseActivity() {
    private val mViewModel by viewModels<WorkOrderRecordViewModel>()

    private val mViewBinding by lazy { ActivityWorkOrderRecordBinding.inflate(layoutInflater) }

    private val workOrderVos by lazy { mutableListOf<WorkOrderVo>() }
    private val wordOrderRecordAdapter by lazy { WordOrderRecordAdapter(this, workOrderVos) }

    override fun getContentView(): View {
        return mViewBinding.root
    }

    override fun getViewModel(): BaseViewModel {
        return mViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        mViewBinding.titleView.setNavigationOnClickListener { onBackPressed() }
        mViewBinding.rcvTask.adapter = wordOrderRecordAdapter
        mViewBinding.rcvTask.addItemDecoration(DefaultItemDecoration(this, LinearLayoutManager.VERTICAL))
        mViewBinding.refreshLayout.setOnRefreshListener {
            mViewModel.getWorkOrderRecord()
        }
        mViewModel.getWorkOrderRecord()
        mViewModel.workOrderLiveData.observe(this) {
            mViewBinding.refreshLayout.closeHeaderOrFooter()
            when (it) {
                is Resource.Success -> {
                    workOrderVos.clear()
                    if (it.data.isNullOrEmpty()) {
                        mViewBinding.emptyLayout.show(EmptyLayout.NO_DATA)
                    } else {
                        workOrderVos.addAll(it.data)
                        mViewBinding.emptyLayout.hide()
                    }
                    wordOrderRecordAdapter.notifyDataSetChanged()
                }
                is Resource.Error -> {
                    mViewBinding.emptyLayout.show(EmptyLayout.LOAD_ERROR)
                    it.throwable?.message?.toast()
                }
            }
        }
    }

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, WorkOrderRecordActivity::class.java)
            context.startActivity(starter)
        }
    }
}